package com.abysscore.yachtdesign.view;

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import com.abysscore.yachtdesign.FreeShipController;

public class FreeShipFileMenu extends JMenu implements ActionListener{
	
	private static final String OPEN = "Open";
	private JMenuItem fileNewMenuItem;
	private JMenuItem fileOpenMenuItem;
	private JMenuItem fileSaveMenuItem;
	private JMenuItem fileSaveAsMenuItem;
	private JMenuItem fileImportMenuItem;
	private JMenuItem fileExportMenuItem;
	private ActionListener listener;
	private FreeShipController control;

	public FreeShipFileMenu(FreeShipController Control, ActionListener freeShipMenuListener) {
		super("File");
		control = Control;
		listener = freeShipMenuListener;
		populateFileMenu();
		
	}

	public FreeShipFileMenu(FreeShipController Control) {
		super("File");
		listener = this;
		control = Control;
		populateFileMenu();
	}

	private void populateFileMenu()
	{
		//fileMenu = new JMenu("File");
	    fileNewMenuItem = new JMenuItem("New...");
	    fileOpenMenuItem = new JMenuItem(OPEN);
	    fileSaveMenuItem = new JMenuItem("Save...");
	    fileSaveAsMenuItem = new JMenuItem("Save As...");
	    fileImportMenuItem = new JMenuItem("Import");
	    fileExportMenuItem = new JMenuItem("Export");
	    fileNewMenuItem.addActionListener(listener);
	    fileOpenMenuItem.addActionListener(listener);
	    fileSaveMenuItem.addActionListener(listener);
	    fileSaveAsMenuItem.addActionListener(listener);
	    fileImportMenuItem.addActionListener(listener);
	    fileExportMenuItem.addActionListener(listener);
	    add(fileNewMenuItem);
	    add(fileOpenMenuItem);
	    add(fileSaveMenuItem);
	    add(fileSaveAsMenuItem);
	    add(fileImportMenuItem);
	    add(fileExportMenuItem);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		System.out.println("Unknown command: " + arg0.getActionCommand());
		if (arg0.getActionCommand().compareTo(OPEN)==0)
		{
			JDialog fod = new JDialog();
			FileDialog fd = new FileDialog(fod, "Open project file", FileDialog.LOAD);
			fd.setFile(".ini");
		    //fd.setDirectory(defDir);
		    fd.setLocation(50, 50);
		    fd.show();
		    System.out.println(" file: " + fd.getDirectory()+ fd.getFile().toString());
		    control.openProjectFile(new File(fd.getDirectory()+fd.getFile()));
		}
		
	}
}
